﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClickNET.Utility
{
    public class StringConversion
    {
        /// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String</param>
        /// <returns>String converted from Unicode Byte Array</returns>
        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            string constructedString = encoding.GetString(characters);
            return constructedString;
        }

        /// <summary>
        /// Converts the String to UTF8 Byte array.
        /// </summary>
        /// <param name="xmlString"></param>
        /// <returns>UTF8 Byte array</returns>
        public static Byte[] StringToUTF8ByteArray(String xmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(xmlString);
            return byteArray;
        }

        /// <summary>
        /// Takes a string seperated by some delimiters and splits it into a generic list
        /// </summary>
        /// <param name="value">string to split</param>
        /// <param name="delimiter">delimiter to split on</param>
        /// <returns>a list of strings</returns>
        public static List<string> SplitStringIntoList(string value, char delimiter)
        {
            List<string> list = new List<string>();

            if (!String.IsNullOrEmpty(value))
            {
                string[] tokens = value.Split(delimiter);

                foreach (string s in tokens)
                {
                    string token = s.Trim();

                    if (!String.IsNullOrEmpty(token))
                        list.Add(token);
                }
            }

            return list;
        }

        /// <summary>
        /// Takes a string and strips out invalid filename characters
        /// </summary>
        /// <param name="name">File name</param>
        /// <returns>Valid file name</returns>
        public static string ConvertIntoValidFileName(string name)
        {
            const char replacementChar = '_';
            string safe = name;
            foreach (char badChar in System.IO.Path.GetInvalidFileNameChars())
            {
                safe = safe.Replace(badChar, replacementChar);
            }
            return safe;
        }

        /// <summary>
        /// Takes a filesize in long, and makes it human readable (KB/MB/GB)
        /// </summary>
        /// <param name="byteCount">bytes</param>
        /// <returns>Human readable string</returns>
        public static string GetHumanReadableFileSize(long byteCount)
        {
            string size = "Size Unknown";
            if (byteCount >= 1073741824)
                size = String.Format("{0:##.##}", byteCount / 1073741824) + " GB";
            else if (byteCount >= 1048576)
                size = String.Format("{0:##.##}", byteCount / 1048576) + " MB";
            else if (byteCount >= 1024)
                size = String.Format("{0:##.##}", byteCount / 1024) + " KB";
            else
                size = byteCount + " Bytes";

            return size;
        }

        /// <summary>
        /// Takes a string and shortens it to a specified length
        /// </summary>
        /// <param name="originalString">string to shorten</param>
        /// <param name="maxLength">max length</param>
        /// <returns>ellipsized text</returns>
        public static string GetEllipsisText(string originalString, int maxLength)
        {
            if (originalString.Length > maxLength)
            {
                originalString = originalString.Substring(0, maxLength);
                originalString += "...";
            }

            return originalString;
        }
    }
}
